package com.sls.nzdapplication.ui.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.Editable;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.TextView;
import android.widget.Toast;

import com.hjq.shape.view.ShapeRadioButton;
import com.hmy.popwindow.PopItemAction;
import com.hmy.popwindow.PopWindow;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.app.AppActivity;
import com.sls.nzdapplication.db.utils.EquipmentUtil;
import com.sls.nzdapplication.db.utils.GyyhdUtil;
import com.sls.nzdapplication.model.Equipment;
import com.sls.nzdapplication.model.EquipmentLine;
import com.sls.nzdapplication.model.Gyyhd;
import com.sls.nzdapplication.myapp.ZDAPP;
import com.sls.nzdapplication.ui.custom.CableWellPhotoView;
import com.sls.nzdapplication.util.Utils;
import com.sls.nzdapplication.util.UuidUtils;

import org.angmarch.views.NiceSpinner;
import org.angmarch.views.OnSpinnerItemSelectedListener;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import es.dmoral.toasty.Toasty;
import me.kareluo.ui.OptionMenu;

/**
 * 1. 概述
 * GyyhdActivity 是一个用于展示和编辑高压用户点的界面。
 *
 *
 * 3. 成员变量
 * | 类型 | 变量名 | 描述 | | --- | --- | --- | |
 * AutoCompleteTextView | autoCompleteTextViewNameView | 高压用户点名称 | | boolean | isNew | 是否为新建高压用户点 |
 * | String | lineId | 所属线路 ID | | Double | lat | 高压用户点纬度 |
 * | Double | lon | 高压用户点经度 |
 * | CableWellPhotoView | equipmentQmPhotoView | 高压用户点前门照片 |
 * | CableWellPhotoView | equipmentMpPhotoView | 高压用户点后门照片 |
 * | TextView | latView | 高压用户点纬度文本框 | | TextView | lonView | 高压用户点经度文本框 |
 * | NiceSpinner | typeView | 高压用户点类型下拉框 | | TextView | saveEquipmentView | 保存高压用户点按钮 |
 * | String | photoType | 照片类型 | | String | equipmentId | 高压用户点 ID | | GyyhdUtil | gyyhdUtil | 高压用户点数据库操作工具 |
 * | String | type | 高压用户点类型 |
 * */
public class GyyhdActivity extends AppActivity implements CableWellPhotoView.OnclickPhotoListener, View.OnClickListener {

    private AutoCompleteTextView autoCompleteTextViewNameView;

    private boolean isNew = true;

    private String lineId = "";

    private Double lat = 0.0;

    private Double lon = 0.0;

    private CableWellPhotoView equipmentQmPhotoView;

    private CableWellPhotoView equipmentMpPhotoView;

    TextView latView,lonView;

    NiceSpinner typeView;

    TextView saveEquipmentView;

    private String photoType;

    private String equipmentId;

    private GyyhdUtil gyyhdUtil;

    private String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_gyyhd;
    }

    @Override
    protected void initView() {

        autoCompleteTextViewNameView = findViewById(R.id.gyyhd_name);
        typeView = (NiceSpinner) findViewById(R.id.gyyhd_type);
        latView = findViewById(R.id.gyyhd_lat);
        lonView = findViewById(R.id.gyyhd_lon);
        equipmentQmPhotoView = findViewById(R.id.gyyhd_qm_photo);
        equipmentQmPhotoView.setOnclickPhotoListener(this);


        equipmentMpPhotoView = findViewById(R.id.gyyhd_mp_photo);
        equipmentMpPhotoView.setOnclickPhotoListener(this);

        saveEquipmentView = findViewById(R.id.save_gyyhd);
        saveEquipmentView.setOnClickListener(this);
        gyyhdUtil = new GyyhdUtil(getContext());
        initType();


    }

    private void initType(){
        List<String> types = new ArrayList<>();
        types.add("高压用户点");
        typeView.attachDataSource(types);

    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if(null !=intent && intent.getStringExtra("equipmentId")!=null){
            isNew = false;
            equipmentId = intent.getStringExtra("equipmentId");
            Gyyhd equipment = gyyhdUtil.listOneGyyhd(equipmentId);
            autoCompleteTextViewNameView.setText(equipment.getName());
            typeView.setText(equipment.getType());
            latView.setText(String.valueOf(equipment.getLat()));
            lineId = equipment.getAreaId();
            lonView.setText(String.valueOf(equipment.getLon()));}
        else if(null !=intent && intent.getBundleExtra("params") != null){
            Bundle bundle = intent.getBundleExtra("params");
            lat = bundle.getDouble("lat");
            lon = bundle.getDouble("lon");
            type = bundle.getString("type");
            typeView.setText(type);
            lineId = bundle.getString("lineId");
            equipmentId = UuidUtils.getStringID();
            latView.setText(String.valueOf(lat));
            lonView.setText(String.valueOf(lon));
        }
        equipmentQmPhotoView.initData("Equipment",equipmentId,"qm");
        equipmentMpPhotoView.initData("Equipment",equipmentId,"mp");
    }

    private void saveData() {
        Gyyhd equipment;
        Editable name = autoCompleteTextViewNameView.getText();
        if (name == null || name.toString().equals("")) {
            Toasty.error(getApplicationContext(), "设备名称不能为空", Toast.LENGTH_LONG, true).show();
            return;
        }
//        if(!isNumber(name.substring(name.length()-1,name.length())) && !type.equals("变电站")){
//            Toasty.error(getApplicationContext(), "设备名称命名不规范", Toast.LENGTH_LONG, true).show();
//            return;
//        }
        if ((gyyhdUtil.queryGyyhdByLineIdAndName(lineId, name.toString()).size() > 0 && isNew) || (gyyhdUtil.queryGyyhdByLineIdAndName(lineId, name.toString()).size() > 0 && !gyyhdUtil.queryGyyhdByLineIdAndName(lineId, name.toString()).get(0).getId().equals(equipmentId))) {
            Toasty.error(getApplicationContext(), "设备已存在", Toast.LENGTH_LONG, true).show();
            return;
        }
        if (typeView.getText().toString().equals("")) {
            Toasty.error(getApplicationContext(), "设备类型不能为空", Toast.LENGTH_LONG, true).show();
            return;
        }

        if (isNew) {
            equipment = new Gyyhd(); // 新创建设备实例
        } else {
            equipment = gyyhdUtil.listOneGyyhd(equipmentId); // 从数据库中获取指定ID的设备
        }

        equipment.setId(equipmentId); // 设置设备ID
        equipment.setName(name.toString()); // 设置设备名称
        equipment.setLon(Double.valueOf(lonView.getText().toString())); // 设置设备经度
        equipment.setLat(Double.valueOf(latView.getText().toString())); // 设置设备纬度
        equipment.setType(typeView.getText().toString()); // 设置设备类型
        equipment.setAreaId(lineId); // 设置设备所属区域ID
        if (isNew) {
            gyyhdUtil.insertGyyhd(equipment); // 向数据库中插入新设备
        } else {
            gyyhdUtil.updateGyyhd(equipment); // 更新已存在的设备信息
        }
        Toasty.success(getApplicationContext(), "保存成功", Toast.LENGTH_LONG, true).show(); // 显示保存成功的提示信息
        isNew = false; // 将isNew标志设置为false，表示设备已保存
    }

    @Override
    public void onRightClick(View v) {
        new PopWindow.Builder(GyyhdActivity.this)
                .setStyle(PopWindow.PopWindowStyle.PopUp)
                .setTitle("你确定要删除当前数据！！")
                .addItemAction(new PopItemAction("确定", PopItemAction.PopItemStyle.Warning, new PopItemAction.OnClickListener() {
                    @Override
                    public void onClick() {
                        clearEquipment();
                    }
                }))
                .addItemAction(new PopItemAction("取消", PopItemAction.PopItemStyle.Cancel))
                .show();
    }

    private void clearEquipment(){
        gyyhdUtil.deleteGyyhd(gyyhdUtil.listOneGyyhd(equipmentId));
        this.finish();
    }


    @Override
    public void onClickPhotoListener(String type) {
        photoType = type;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(photoType.equals("qm")) {
            equipmentQmPhotoView.onActivityResultListener(requestCode, resultCode, data);
        }
        if(photoType.equals("mp")) {
            equipmentMpPhotoView.onActivityResultListener(requestCode, resultCode, data);
        }
    }


    @Override
    public void openPhotoView(String path) {
        Intent intent = new Intent(GyyhdActivity.this,PhotoDetailActivity.class);
        intent.putExtra("filePath",path);
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        if(view == saveEquipmentView){
            saveData();
        }

    }
}